/*
 * 工具类结合jquery,在页面中定义个table,使用给定的数据形成table的title,result结果。
 * 展示表格的查询列和查询结果
 * 提供查询结果行的简易操作接口
 * */
(function QTable(window){

//保存window的属性，新建QTable对象
var 
	QTable = {},
	
	// 存放所有通过控件初始化过的对象，使用局部变量来控制，拒绝外部对此变量的操作
	List = [];

window.QTable = QTable;		//在window上的引用

/**
 * 工具对象
 */
QTable.Util = {};

/**
 * 设置默认配置条件
 */
QTable.option = {
		
	/*
	 * 设置表格的配置参数
	 * class:表格样式类别，用于设置表格的样式
	 */
	"class" : "QTable_table",
	
	/*
	 * 设置标题中除全选外，其它项配置
	 * itemClass:标题项中，子项的class，用于设置排序时的图标
	 * */
	head : {
	
		itemClass : "sort",
		
		/*
		 * 设置是否可排序 
		 */
		sortable : true,
		
		selectable : true,
		/*
		 * 设置标题全选按钮
		 * require(true/false):是否需要全选按钮
		 * disabled(true/false):是否按钮失效
		 * id:checkbox的id
		 * class:设置th的样式
		 * */
		selectAll : {
			
			require : true,					//有全选按钮
			disabled : false,				//全选按钮是否失效
			id : "selectAll",				//全选按钮id
			"class" : "check"
		}
	},
	
	blank : true,
	blankClass : "QTable_blank",
	
	/*
	 * 行配置
	 * selectable(true/false):行是否可选
	 * */
	selectable : true,
	
	/*
	 * 设置查询结果每个单元格的配置
	 * type["text","check","a"]:text单元格将显示内容直接存放在单元，check单元格形成一个checkbox，a单元格
	 * 根据显示内容的数量形成a标签
	 */
	row : {
		selectable : true,
		"class" : "resultSet",
		"selectedClass" : "selected",
		type : "text"
	},
	
	foot : {
		require : true,
		pageNum : 1,	//默认初始页
		pageSize : 10,	//默认页码值
		"class" : "pagination"
	}
}

/**
 * 表格对象的构造方法
 */
function table(_DOM, _option){
	
	var option = $.extend(true, {}, QTable.option);	// 深层复制配置
	/*订制配置，覆盖默认的配置信息*/
	if(_option) {
		option = $.extend(true, option, _option);
	}
	//方法必需放置在这，new head/foot需要调用到
	this.getOption = function(){
		return option;
	}
	
	var $DOM = _DOM || $("<table>"),					//设置局部变量(下文中对this.$DOM使用较多)
		rows = [],								//表格行数据
		rowIds = [],							//表格行数据id
		rowData = [],
		selected = [],
		selected_Change_Fn,
		QHead = new head(this, option.head),
		QFoot = new foot(this, option.foot);
	
	this.getDOM = function(){
		return $DOM;
	};
	
	/*获取行*/
	this.getRows = function(){
		return rows;
	}
	
	/*获取行ID*/
	this.getRowIds = function(){
		return rowIds;
	}
	
	this.getRowData = function(){
		return rowData;
	}
	
	this.getSelected = function(){
		return selected;
	}
	
	this.getHead = function(){
		return QHead;
	}
	
	this.getFoot = function(){
		return QFoot;
	}
	
	$DOM.addClass(option["class"]);							//设置表格样式
}

(function(){
	
	var proto = {
		getOption : function(){},
		getDOM : function(){},
		getHead : function(){},
		getFoot : function(){},
		getRows : function(){},
		getRowIds : function(){},
		getRowData : function(){},
		getSelected : function(){},
		getHead : function(){},
		getFoot : function(){},
		getHeadOption : function(){
			return this.getOption().head;
		},
		getRowOption : function(){
			return this.getOption().row;
		},
		getFootOption : function(){
			return this.getOption().foot;
		},
		defalut_Selected_Change_Fn : function(_rowId){}
	};
	
	/*
	 * 刷新标题栏
	 * */
	proto.refreshHead = function (_data){
		
		if(!_data || !$.isArray(_data)) return this;
		
		this.getHead().clear();				//清空内容
		_data.QID = "head";					//添加ID
		this.getHead().refresh(_data);
		this.getDOM().prepend(this.getHead().getDOM());	//添加新对象
		return this;
	}
	
	/*
	 * 刷新表格数据
	 * 
	 * 
	 * @param _data	原始数据,原始数据是一个数组类型数据，数组中的元素将用来形成一条表格行数据
	 * [{
	 * 	id : 1, name: "test"
	 * },
	 * {
	 *  id : 2, name : "abc"
	 * }]
	 * @param _display	转义数据
	 * @param _column	原始数据中的展示列
	 * @param _option	配置项
	 * @return
	 */
	proto.refresh = proto.refreshRows = function(_data){
		
		if(!_data || !_data.list) return this;
		
		this.setHeadStatus(false);
		//清空原有内容
		for(var i=0, len=this.getRows().length; i<len; i+=1){
			
			if($.inArray(this.getRows()[i].getId(), this.getSelected()) != -1){
				this.delSelected(this.getRows()[i].getId());
			}
			this.getRows()[i].clear();
		}

		$(".QTable_blank").parent().parent().remove();
		
		this.getRows().length = 0;
		
		this.addRows(_data.list);
		
		var rows = this.getRows();
		if(rows.length < 1){
			var blank = $("<tr><td><p style='color:#878787; margin:32px 0px; text-align:center;'>没有查询到相关记录</p></td></tr>");
			this.showBlank();
		} else {
			rows[rows.length - 1].getDOM().addClass("last");
		}
		
		if(!this.getOption().foot.require) return this;	//如果不需要页码，取消显示
		this.refreshFoot(_data);
		return this;
	}
	
	/*提加空白*/
	proto.showBlank = function(_class){
		
		var $tr = $("<tr>"),
			$td = $("<td>"),
			$p = $("<p>没有查询到相关记录</p>").addClass("QTable_blank"),
			colspan = 0;
		
		if(this.getRowOption().selectable){
			colspan = 1;
		}
		
		if(this.getHead().getData()){
			colspan += this.getHead().getData().length;
		}
		
		$td.attr("colspan", colspan);
		this.getDOM().append($tr.append($td.append($p)));
	}
	
	/*添加行 */
	proto.addRow = function(_data){
		
		_data.QID = _data.id || this.newRowId();
		
		var pos,											//首个元素添加位置(head后)
			newRow = new row(this, _data, this.getOption().row),
			rows = this.getRows(),
			rowIds = this.getRowIds();//创建行数据
		
		if(this.getRows().length == 0){								//不存在任何行数据
			
			rows[0] = newRow;
			rowIds[0] = newRow.getId();
			pos = this.getHead();
		} else {
			
			pos = rows[rows.length - 1];
			rows[rows.length] = newRow;
			rowIds[rows.length - 1] = newRow.getId();		//rows已经自增完毕，所以要减去刚才的1个元素
		}
		pos.getDOM().after(newRow.getDOM());
		
		if(rows.length > 1){
			rows[rows.length - 2].getDOM().removeClass("last");
		}
		rows[rows.length - 1].getDOM().addClass("last");
	}
	
	/*添加行 */
	proto.addRows = function(_data){
		
		if(!_data || !$.isArray(_data)) return this;
		for(var i=0, len=_data.length; i<len; i+=1){
			this.addRow(_data[i]);
		}
	}
	
	/*删除行*/
	proto.deleteRow = function(_rowId){
		
		var index = $.inArray(_rowId, this.getRowIds());
		if(index != -1){
			
			this.getRows().splice(index, 1);
			this.getRowIds().splice(index, 1);
			this.getDOM().find("tr[QRowID='" + _rowId +"']").remove();
		}
	}
	
	/*获取标题状态，即全选按钮状态*/
	proto.setHeadStatus = function(_flag){
		this.getHead().setStatus(_flag);
	}
	
	/*刷新页码内容*/
	proto.refreshFoot = function(_data){
		this.getFoot().clear();
		this.getFoot().refresh(_data);
		this.getDOM().after(this.getFoot().getDOM());
	}
	
	/*随机行ID */
	proto.newRowId = function(){
		
		var id = 1;
		while($.inArray(id, this.getRowIds()) != -1){			//判断1是否存在，不存在使用1
			id = Math.floor(Math.random()*100+1);				//生成1-100的随机数
		}
		return id;
	}
	
	/*获取行*/
	proto.getRow = function(_rowId){
		
		var index = $.inArray(_rowId, rowIds);
		if(index != -1){
			
			return this.getRows()[index];
		}
	}
	
	/*设置行的可选状态*/
	proto.rowSelectable = function(_rowId, _able){
		
		if(!this.getOption().selectable) return;
		
		var index = $.inArray(_rowId, this.getRowIds());
		if(index != -1){
			
			this.getRows()[index].selectable(_able);
		}
	}
	
	/*获取勾选行*/
	proto.getSelectedRow = function(){
		
		var result = [],
			selected = this.getSelected(),
			rows = this.getRows();
		for(var i=0, len=selected.length; i<len; i+=1){
			$.inArray(selected[i], rowIds) != -1 && result.push(rows[selected[i]]);
		}
		return result;
	}
	
	/*添加被选项ID*/
	proto.addSelected = function(_rowId){
		
		$.inArray(_rowId, this.getSelected()) == -1 && this.getSelected().push(_rowId);
		selected_Change_Fn(_rowId, "add");
	}
	
	/*删除被选项ID*/
	proto.delSelected = function(_rowId){
		
		this.getSelected().splice($.inArray(_rowId, this.getSelected()), 1);
		selected_Change_Fn(_rowId, "delete");
	}
	
	/*选择行 */
	proto.selectRow = function(_rowId){
		
		if(!this.getOption().selectable) return;
		
		var index = $.inArray(_rowId, this.getRowIds());
		if(index != -1){
			
			if(!this.getRows()[index].doSelect()) return;			//如果行不可选，不进行选择
			
			this.addSelected(_rowId);
		}
//		this.onSelectRow(_rowId);	//向外发送事件
	}
	
	/*取消行选择 */
	proto.deselectRow = function(_rowId){
		
		if(!this.getOption().selectable) return;
		var index = $.inArray(_rowId, this.getRowIds());
		if(index != -1){
			
			this.getRows()[index].doDeselect();
			
			//删除记录已选行ID
			this.delSelected(_rowId);
		}
//		this.onDeselectRow(_rowId);
	}
	
	/*全选行内容*/
	proto.selectAll = function(){
		
		if(!this.getOption().selectable) return;
		
		if(this.getRowIds().length < 1) return;
		
		for(var i=0, len=this.getRowIds().length; i<len; i+=1){
			this.selectRow(this.getRowIds()[i]);
		}
	}
	
	/*取消全选*/
	proto.deselectAll = function(){
		
		if(!this.getOption().selectable) return;
		
		if(this.getRowIds().length < 1) return;
		
		for(var i=0, len=this.getRowIds().length; i<len; i+=1){
			this.deselectRow(this.getRowIds()[i]);
		}
	}
	
	/*获取每页数据数量 */
	proto.getPageSize = function(){
		return this.getFoot().getPageSize();}
	
	/* 获取当前页码 */
	proto.getPageNum = function(){
		return this.getFoot().getPageNum();}
	
	/*获取数据总数 */
	proto.getTotal = function(){
		return this.getFoot().getTotal();}
	
	/*是否可选*/
	proto.selectable = function(_flag){
		
		if(_flag != undefined) {
			
			this.getOption().selectable = !!_flag;
			this.getHead().selectable(_flag);
			for(var i=0, len=this.getRows().length; i<len; i+=1){
				this.getRows()[i].selectable(_flag);
			}
		}
		
		return this.getOption().selectable;
	}
	
	/*添加页面跳转方法*/
	proto.onPageSkip = function(_fn, _exec){
		
		this.getFoot().onPageSkip(_fn, _exec);
	}
	
	/*添加selected变量监听*/
	selected_Change_Fn = proto.defalut_Selected_Change_Fn;
	proto.onSelectedChange = function(_fn, _exec){
		
		var caller = this;
		//默认执行以下内容
		if(_exec != undefined && !_exec){
			
			selected_Change_Fn = _fn;
			
		} else {
			
			selected_Change_Fn = function(){
				caller.defalut_Selected_Change_Fn.apply(caller, arguments);
				_fn.apply(caller, arguments);
			};
		}
	}
	
	proto.getOrderBy = function(){
		return this.getHead().getOrderBy();
	}
	
	proto.getOrder = function(){
		return this.getHead().getOrder();
	}
	
	proto.orderBy = function(_name, _order){
		return this.getHead().orderBy(_name, _order);
	}
	
	proto.onOrderBy = function(_fn, _exec){
		this.getHead().onOrderBy(_fn, _exec);
	}
	
	table.prototype = proto;
})();

/**
 * 表格标题构造方法
 * _parent : table对象
 * _data : 声明列属性的对象数组，列对象属性包括name:列名称,title:列标题,type:列数据类型
 */
function head(_parent, _option){
	
	var	parent = _parent,						//设置局部变量(下文中对this.$DOM使用较多)
		$DOM = $("<tr>"),						//保存局部变量
		data = {},
		option = $.extend(true, {}, _option),	// 深层复制配置
		$checkBox = $("<input type='checkbox' />"),
		order = "ASC",
		orderBy = "",
		orderBy_Fn = function(){},
		default_orderBy_Fn = function(){};	
	
	this.getParent = function(){
		return parent;
	}
	
	this.getData = function(){
		return data;
	}
	
	this.getOption = function(){
		return option;
	}
	
	this.getDOM = function(){
		return $DOM;
	}
	
	this.getCheckBox = function(){
		return $checkBox;
	}
	
	this.getOrder = function(){
		return order;
	}
	
	this.getOrderBy = function(){
		
		return orderBy;
	}
	
	this.refresh = function(_data){
		
		var QID = _data.QID;
		$DOM.attr("QID", QID);
		
		if(option.selectAll.require){
			
			//创建装载全选按钮的td,并设置class=check
			var $th = $("<th>");
			$th.addClass(option.selectAll["class"]);	
			
			//创建全选按钮
			var $checkBox = this.getCheckBox();
			$checkBox.attr("QID", option.selectAll.id);		
			$checkBox.prop("disabled", !option.selectable);					//设置全选按钮的失效状态
			$checkBox.bind("click", {parent: parent}, function(event){		//绑定点击事件
				
				var parent = event.data.parent;					//获取对table的引用
				if($(this).prop("checked")){					//刚处于全选状态
					parent.selectAll();
				} else {										//点击事件是在check事件之后
					parent.deselectAll();
				}
			});
			
			$DOM.append($th.append($checkBox));					//将checkbox添加到td，td添加到tr中
		}
		
		for(var i=0, len=_data.length; i<len; i+=1){
			
			var name = _data[i].name,
				title = _data[i].title,
				type = _data[i].type,
				$th = $("<th>"),
				$a = $("<a href='javascript:;'>").addClass(option["itemClass"]);		//创建内容
			
			$th.width(_data[i].width);
			$a.attr("QCol", name).attr("QType", type).attr("QSeq", i).html(title);
			
			//添加排序事件
			if(option.sortable){
				$a.bind("click",{head: this},function(event){
					
					var head = event.data.head,
						$this = $(this),
						col = $this.attr("QCol");
					
					if($this.hasClass("DESC")){
						
						$this.removeClass("DESC").addClass("ASC");
						order = "ASC";
						orderBy = col;
					} else {
						
						head.getDOM().find("a.ASC").removeClass("ASC");
						head.getDOM().find("a.DESC").removeClass("DESC");
						
						$this.addClass("DESC");
						order = "DESC";
						orderBy = col;
					}
					
					orderBy_Fn(orderBy);
				});
			}
			
			if(type == "text"){
				$DOM.append($th.html(title));
			} else {
				$DOM.append($th.append($a));
			}
		}
		
		data = $.extend(true, [], _data);
	}

	this.clear = function(){
		$DOM.remove();
	};
	
	this.onOrderBy = function(_fn, _exec){
		
		var caller = this;
		//默认执行以下内容
		if(_exec != undefined && !_exec){
			
			orderBy_Fn = _fn;
			
		} else {
			
			orderBy_Fn = function(){
				default_orderBy_Fn.apply(caller, arguments);
				_fn.apply(caller, arguments);
			};
		}
	}
	
	this.orderBy = function(_name, _order){
	
		if(_order.toUpperCase() == "ASC"){
			order = "ASC";
		} else if(_order.toUpperCase() == "DESC"){
			order = "DESC";
		} else {
			return;
		}
		orderBy = _name;
		$DOM.find("a[QCol=\""+ _name +"\"]").removeClass("DESC").removeClass("ASC").addClass(order);
	}
}

(function(){
	
	var proto = {
			getOption : function(){},	//object,配置参数对象
			getParent : function(){},	//object,配置参数对象
			getOption : function(){},	//object,配置参数对象
			getParent : function(){},	//table,父类的引用
			getDOM : function(){},		//DOM,当前对象的DOM引用
			getCheckBox : function(){}	//DOM,全选框的DOM引用
		};				//设置一次就不设置，减少new对象时调用这段代码
	
	/*设置全选按钮状态*/
	proto.setStatus = function(_flag){
		
		if(_flag != undefined) {
			
			this.getCheckBox().attr("checked", _flag);
		}
	}
	
	/*是否可选*/
	proto.selectable = function(_flag){
		
		if(_flag != undefined) {
			
			this.getOption().selectable = !!_flag;
			this.getOption().selectAll.require && this.getCheckBox().prop("disabled", !this.getOption().selectable);
		}
		
		return this.getOption().selectable;
	}
	
	head.prototype = proto;
})();

/**
 * 表格正文构造方法
 */
function row(_parent, _data, _option){
	
	var parent = _parent,						//设置局部变量(下文中对this.$DOM使用较多)
		$DOM = $("<tr>"),						//保存局部变量
		option = $.extend(true, {}, _option),	// 深层复制配置
		data = $.extend(true, {}, _data),
		col = parent.getHead().getData(),
		items = [],
		itemSeq = [];
	
	this.getParent = function(){
		return parent;
	}
	
	this.getDOM = function(){
		return $DOM;
	}
	
	this.getOption = function(){
		return option;
	}
	
	this.getData = function(){
		return data;
	}
	
	this.getItems = function(){
		return items;
	}
	
	this.getItem = function(_colName){
		
		for(var i=0, len=itemSeq.length; i<len; i++){
			if(itemSeq[i] == _colName) break;
		}
		
		if(i < len) return items[i];
	}
	
	this.refresh = function(_data){			//涉及到对闭包属性的更改，如法放置在方法外部
		
		var QID = _data.QID;
		$DOM.attr("QRowID", QID);												//设置QID
		
		if(this.getOption().selectable){										//table设置可选，添加选择按钮
			
			var $td = $("<td>");
			
			//设置行是否可选
			var $checkbox = $("<input type='checkbox' />").attr("QID", "selectBtn").prop("disabled", !this.getOption().selectable);
			$checkbox.bind("click", {parent: this.getParent(), QID: QID}, function(event){
				
				if($(this).prop("checked")){					//刚处于全选状态
					event.data.parent.selectRow(event.data.QID);
				} else {										//点击事件是在check事件之后
					event.data.parent.deselectRow(event.data.QID);
				}
			});
			
			items[0] = $td.get(0);
			
			//添加到行内
			$DOM.append($td.append($checkbox));
			
			//清空临时变量
			$checkbox = $td = undefined;									
		}
		
		for(var i=0, len=col.length; i<len; i+=1){
			
			var $td = $("<td>"), name = col[i]["name"], type = col[i]["type"] || "text";
			if(type != "text"){
				
			} else if(type == "object"){
				
			} else {
				
				var html = _data;
				if(name.indexOf(".", 0) == -1){
					html = html[name];
				} else {
					var tmp = name.split(".");
					for(var j=0, len2=tmp.length; j<len2; j+=1){
						
						if(html[tmp[j]] == undefined){
							html = "";
							break;
						}
						html = html[tmp[j]];
					}
				}
				$td.html(html);
			}
			$DOM.append($td);
			
			itemSeq[items.length] = name;
			items[items.length] = $td.get(0);
		}
		
		data = $.extend(true, {}, _data);
	}
	
	this.clear = function(){
		$DOM.remove();
	}
	
	if(data){
		this.refresh(data);
	}
	
	$DOM.addClass(option["class"]);				//设置行属性
}

(function(){
		
	var proto = {
		
		getDOM : function(){},
		getData : function(){},		//object,行数据对象
		getParent : function(){},	//table,表格对象,当前对象的父容器
		getOption : function(){},	//object,配置信息
		getId : function(){
			return this.getData().QID;
		},
		clear : function(){			//清空内容，对闭包内容的更改无法放置在闭包外头，所以这里没办法实现，即使去实现，也无法修改闭包中.
		}		
	};
	
	proto.doSelect = function(){
		
		//行不可选时取消选择
		if(!this.getOption().selectable) return false;
		
		//取消行选中状态
		this.getDOM().addClass(this.getOption()["selectedClass"]);
		this.getDOM().find("input[QID='selectBtn']").not(":disabled").prop("checked", true);
		return true;
	}
	
	proto.doDeselect = function(){
		
		//行不可选时取消选择
		if(!this.getOption().selectable) return;
		
		//设置行选中状态
		this.getDOM().removeClass(this.getOption()["selectedClass"]);
		this.getDOM().find("input[QID='selectBtn']").not(":disabled").prop("checked", false);
		return true;
	}
	
	/*设置行是否可选*/
	proto.selectable = function(_flag){
		
		if(_flag != undefined) {
			
			this.getOption().selectable = !!_flag;
			this.getDOM().find("input[QID='selectBtn']").prop("disabled", !this.getOption().selectable);
		}
		
		return this.getOption().selectable;
	}
	
	row.prototype = proto;
})();

/**
 * 表格底部构造方法
 */
function foot(_parent, _data, _option){
	
	var $DOM = $("<div/>"),
		parent = _parent,
		data = _data,
		option = $.extend(true, {}, _option || parent.getFootOption()),// 深层复制配置
		pageNum = option.pageNum,	//当前页数，不能删除变量
		pageSize = option.pageSize, //默认每页显示数目
		page_Skip_Fn = function(){},
		default_Page_Skip_Fn = function(_pageNum){	//默认跳转方法
			pageNum = _pageNum;
		};
		
	page_Skip_Fn = this.default_Page_Skip_Fn;
	
	this.getDOM = function(){
		return $DOM;
	}
	
	this.getData = function(){
		return data;
	}
	
	this.getParent = function(){
		return parent;
	}
	
	this.getOption = function(){
		return option;
	}
	
	/*获取每页数据数量 */
	this.getPageSize = function(){return pageSize;}
	
	/* 获取当前页码 */
	this.getPageNum = function(){return pageNum;}
	
	/*获取数据总数 */
	this.getTotal = function(){return total;}
	
	/*获取页数*/
	this.getCount = function(){return count;}

	/*刷新内容 */
	this.refresh = function(_data){
		
		//配置值
		if(_data.pageNum != undefined){
			pageNum = _data.pageNum;
		} else {
			pageNum = option.pageNum;
		}
		
		if(_data.pageSize != undefined){
			pageSize = _data.pageSize;
		} else {
			pageSize = option.pageSize;
		}
		
		if(_data.total != undefined){
			total = _data.total;
		} else {
			total = option.total;
		}
		
		count = Math.ceil(total/pageSize);
		
		//添加跳转到最前页的按钮
		if(pageNum <= 1){
			$("<span>").addClass("firstPage").html("&nbsp;").appendTo($DOM);
		} else {
			var $a = $("<a>").addClass("firstPage").html("&nbsp;").attr("href", "javascript:;");
			$a.bind("click", {page: this, pageNum: 1}, function(event){
				page_Skip_Fn(event.data.pageNum);
			});
			$a.appendTo($DOM);
		}
		
		//添加前一页按钮
		if(pageNum <= 1){
			$("<span>").addClass("previousPage").html("&nbsp;").appendTo($DOM);
		} else {
			var $a = $("<a>").addClass("previousPage").html("&nbsp;").attr("href", "javascript:;");
			$a.bind("click", {page: this, pageNum: pageNum-1}, function(event){
				page_Skip_Fn(event.data.pageNum);
			});
			$a.appendTo($DOM);
		}
		
		if(pageNum > 3){
			$("<span>").addClass("pageBreak").html("...").appendTo($DOM);
		}
		
		for(var i=pageNum - 2; i <= pageNum + 2; i+=1){
			
			if(i < 1 || i > count) continue;
			
			if(i != pageNum){
				var $a = $("<a>").html(i).attr("href", "javascript:;");
				$a.bind("click", {page: this, pageNum: i}, function(event){
					page_Skip_Fn(event.data.pageNum);
				});
				$a.appendTo($DOM);
			} else {
				$("<span>").addClass("currentPage").html(i).appendTo($DOM);
			}
		}
		
		if(pageNum + 2 < count){
			$("<span>").addClass("pageBreak").html("...").appendTo($DOM);
		}
		
		//添加下一页按钮
		if(pageNum < count){
			
			var $a = $("<a>").addClass("nextPage").html("&nbsp;").attr("href", "javascript:;");
			$a.bind("click", {page: this, pageNum: pageNum+1}, function(event){
				page_Skip_Fn(event.data.pageNum);
			});
			$a.appendTo($DOM);
		} else {
			$("<span>").addClass("nextPage").html("&nbsp;").appendTo($DOM);
		}
		
		//添加最后一页按钮
		if(pageNum < count){
			
			var $a = $("<a>").addClass("lastPage").html("&nbsp;").attr("href", "javascript:;");
			$a.bind("click", {page: this, pageNum: count}, function(event){
				page_Skip_Fn(event.data.pageNum);
			});
			$a.appendTo($DOM);
		} else {
			$("<span>").addClass("lastPage").html("&nbsp;").appendTo($DOM);
		}
		
		//添加跳转
		var $span = $("<span>").addClass("pageSkip");
		$span.append("共" + count + "页 到");
		$input = $("<input QID='pageNum' maxlength='9' onpaste='return false;' />");
		$input.val(pageNum);
		$span.append($input);
		$btn = $("<button>").html("&nbsp;");
		$btn.bind("click", {page: this, count : count}, function(event){
			var pageNum = $input.val() > event.data.count ? event.data.count : $input.val();
			pageNum = pageNum > 0 ? pageNum : 1;
			page_Skip_Fn(pageNum);
		});
		$span.append($btn);
		$DOM.append($span);
		data = _data;
	}
	
	this.onPageSkip = function(_fn, _exec){
		
		var caller = this;
		//默认执行以下内容
		if(_exec != undefined && !_exec){
			
			page_Skip_Fn = _fn;
			
		} else {
			
			page_Skip_Fn = function(pageNum){
				default_Page_Skip_Fn.apply(caller, arguments);
				_fn.apply(caller, arguments);
			};
		}
	}
	
	this.pageSkip = function(_pageNum){
		page_Skip_Fn(_pageNum);
	}
	
	this.clear = function(){ 
		$DOM.empty();
	}
	
	$DOM.addClass(option["class"]);				//设置行属性
}

/**
 * 初始化
 */
QTable.init = function(_pk, _option){
	
	var tab = new table($(_pk), _option);
	List[_pk] = List.length;				//索引，其实没必要，直接用对象的map性质存储也是可以的，这里为了让代码更好理解
	List.push(tab);							//保存创建的对象
	return tab;								//初始化完成，返回对象
}

/**
 * 获取QTable对象，若对象不存在，将DOM对象转化成QTable对象
 */
QTable.get = function(_pk){
	return List[List[_pk]];
}
})(window);